package org.monkey.common.generator.formatter.entity.form;

import cn.hutool.core.util.StrUtil;
import java.io.File;
import java.util.List;
import org.monkey.common.constant.util.MonkeyIoUtil;
import org.monkey.common.constant.util.MonkeyStrUtil;
import org.monkey.common.generator.entity.GeneratorConfig;
import org.monkey.common.generator.entity.JavaEntityInfo;
import org.monkey.common.generator.service.TemplateFileFormatter;
import org.monkey.common.generator.util.FormatUtil;
import org.springframework.stereotype.Component;

@Component
public class AddFormTemplateFileFormatter implements TemplateFileFormatter {

  private String templateStr = null;

  @Override
  public String getTemplateStr() {
    if(StrUtil.isBlank(templateStr)){
      templateStr = loadTemplateStr("add.ftl");
    }
    return templateStr;
  }


  @Override
  public String getTemplateName() {
    return "add";
  }

  @Override
  public void format(File rootDir, List<JavaEntityInfo> javaEntityInfos, GeneratorConfig config) {
    String dtoDir =
        rootDir.getAbsolutePath()
            + "/"
            + config.getModuleName()
            + "/src/main/java/"
            + config.getPackageName().replace(".", "/")
            + "/entity/form";
    MonkeyIoUtil.batchCreateDirs(dtoDir);
    for (JavaEntityInfo entity : javaEntityInfos) {
      String dtoFile =
          dtoDir + "/" + MonkeyStrUtil.upperFirstCase(entity.getEntityName()) + "AddForm.java";
      FormatUtil.format(getTemplateStr(), dtoFile, config.toParamMap(entity));
    }
  }
}
